package com.zs.backend.common;

import com.zs.backend.common.utils.BeanUtil;
import com.zs.backend.domain.AdminUserDetails;
import com.zs.backend.modules.ums.dto.UserDTO;
import com.zs.backend.modules.ums.model.UmsAdmin;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

public class UserHolder {

    public static UmsAdmin getCurrentAdmin() {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (principal instanceof UserDetails) {
            UserDetails userDetails = (UserDetails) principal;
            if (userDetails instanceof AdminUserDetails) {
                AdminUserDetails adminUserDetails = (AdminUserDetails) userDetails;
                return adminUserDetails.getUmsAdmin();
            }
        }
        return new UmsAdmin();
    }

    public static UserDTO getCurrentAdmins() {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (principal instanceof UserDetails) {
            UserDetails userDetails = (UserDetails) principal;
            if (userDetails instanceof AdminUserDetails) {
                AdminUserDetails adminUserDetails = (AdminUserDetails) userDetails;
                UmsAdmin umsAdmin = adminUserDetails.getUmsAdmin();
                UserDTO userDTO = BeanUtil.beanCopyProperties(umsAdmin, UserDTO.class);
                userDTO.setUmsRoleList(adminUserDetails.getUmsRoleList());
                return userDTO;
            }
        }
        return new UserDTO();
    }


}
